package com.bruce.learnrecylerview.app.ui.base;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.Collections;
import java.util.List;

/**
 * Created by Bruce-Home on 2015/8/24.
 */
@Deprecated
public class ItemTouchBuilder extends ItemTouchHelper.Callback {

    private int dragFlags;
    private int swipeFlags;
    private onMoveListener moveListener;
    private onSwipedListener swipedListener;

    private ItemTouchBuilder() {
    }
    private ItemTouchBuilder(int dragFlags, int swipeFlags) {
        this.dragFlags = dragFlags;
        this.swipeFlags = swipeFlags;
    }

    public ItemTouchBuilder setMove(onMoveListener listener) {
        moveListener = listener;
        return this;
    }

    public ItemTouchBuilder setSwiped(onSwipedListener listener) {
        swipedListener = listener;
        return this;
    }

    public static ItemTouchBuilder builder(int dragFlags, int swipeFlags) {
        return new ItemTouchBuilder(dragFlags, swipeFlags);
    }
    public static ItemTouchBuilder builder() {
        return new ItemTouchBuilder(0, 0);
    }

    public void attachToRecyclerView(RecyclerView recyclerView) {
         new ItemTouchHelper(this).attachToRecyclerView(recyclerView);
    }

    public ItemTouchBuilder dragDown() {
        dragFlags |= ItemTouchHelper.DOWN;
        return this;
    }
    public ItemTouchBuilder dragUp() {
        dragFlags |= ItemTouchHelper.UP;
        return this;
    }
    public ItemTouchBuilder swipeStart() {
        swipeFlags |= ItemTouchHelper.START;
        return this;
    }
    public ItemTouchBuilder swipeEnd() {
        swipeFlags |= ItemTouchHelper.END;
        return this;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return dragFlags > 0;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return swipeFlags > 0;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (moveListener != null) {
            return moveListener.onMove(recyclerView, viewHolder, target);
        }
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (swipedListener != null) {
            swipedListener.onSwiped(viewHolder, direction);
        }
    }

    public interface onMoveListener {
        boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target);
    }

    public interface onSwipedListener {
        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
    }

    public static void simpleMoveSwap(RecyclerView.Adapter adapter, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target, List mItems) {
        int fromPosition = viewHolder.getAdapterPosition();
        int toPosition = target.getAdapterPosition();
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mItems, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mItems, i, i - 1);
            }
        }
        adapter.notifyItemMoved(fromPosition, toPosition);
    }

    public static void simpleSwipeRemove(RecyclerView.Adapter adapter, RecyclerView.ViewHolder viewHolder, List mItems) {
        mItems.remove(viewHolder.getAdapterPosition());
        adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
    }

    public ItemTouchBuilder setSimpleMoveSwap(RecyclerView.Adapter adapter, List items, OnFinishListener listener) {
        moveListener = (recyclerView, viewHolder, target) -> {
            simpleMoveSwap(adapter, viewHolder, target, items);
            if (listener != null) {
                listener.onFinish(viewHolder);
            }
            return true;
        };
        return this;
    }

    public ItemTouchBuilder setSimpleSwipeRemove(RecyclerView.Adapter adapter, List items, OnFinishListener listener) {
        swipedListener = ((viewHolder, direction) -> {
            simpleSwipeRemove(adapter, viewHolder, items);
            if (listener != null) {
                listener.onFinish(viewHolder);
            }
        });
        return this;
    }

    public ItemTouchBuilder setSimpleSwapAndRemove(RecyclerView.Adapter adapter, List items) {
        setSimpleSwipeRemove(adapter, items, null);
        setSimpleMoveSwap(adapter, items, null);
        return this;
    }

    public interface OnFinishListener {
        void onFinish(RecyclerView.ViewHolder viewHolder);
    }
}
